package vip.xiaonuo.tasks;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import vip.xiaonuo.core.timer.TimerTaskRunner;
import vip.xiaonuo.facedoor.modular.devicebase.entity.DeviceBase;
import vip.xiaonuo.facedoor.modular.devicebase.service.DeviceBaseService;
import vip.xiaonuo.facedoor.modular.memberdevice.entity.MemberDevice;
import vip.xiaonuo.facedoor.modular.memberdevice.service.MemberDeviceService;

import java.util.List;
import java.util.stream.Collectors;

@Component
@Slf4j
public class NoDeviceMemberTask implements TimerTaskRunner {

  @Autowired
  private DeviceBaseService deviceBaseService;

  @Autowired
  private MemberDeviceService memberDeviceService;


  @Override
  public void action(String taskId,String taskParam) {
    execute();
  }

  private void execute() {
    log.info("剔除无用人员- 设备 关联关系");
    List<DeviceBase> deviceBases = deviceBaseService.list();
    if (CollectionUtils.isNotEmpty(deviceBases)) {
      List<Long> collect = deviceBases.stream().map(DeviceBase::getId).collect(Collectors.toList());
      memberDeviceService.remove(new LambdaQueryWrapper<MemberDevice>().notIn(MemberDevice::getDeviceId, collect));
    }
  }

  @Override
  @XxlJob("NoDeviceMemberTask")
  public void executeXxlJob() {
    execute();
  }

}
